package com.zg.ktcomwan.adapter.state

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import com.zg.ktcomwan.databinding.RvHeaderLayoutBinding

/***
 * @Author:      zxd
 * @CreateDate:  2021/8/16 8:35
 * @Description: 文章的刷新头部
 */
class ArticleHeaderLoadStateAdapter :
    LoadStateAdapter<ArticleHeaderLoadStateAdapter.ViewHolder>() {
    class ViewHolder(val binding: RvHeaderLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(loadState: LoadState) {
            when (loadState) {
                //加载失败
                is LoadState.Error -> {
                    binding.header.visibility = View.GONE
                    binding.errorTv.visibility = View.VISIBLE
                    binding.retryTv.visibility = View.VISIBLE
                    binding.retryTv.setOnClickListener {
                        //adapter.retry()
                    }
                }
                //正在加载中
                is LoadState.Loading -> {
                    binding.header.visibility = View.VISIBLE
                }
                is LoadState.NotLoading -> {
                    binding.header.visibility = View.GONE
                    binding.errorTv.visibility = View.GONE
                    binding.retryTv.visibility = View.GONE
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, loadState: LoadState) {
        holder.bindData(loadState)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ViewHolder {
        val binding =
            RvHeaderLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }
}